import 'package:flutter/material.dart';
import 'package:osc/common/constants.dart';
import 'package:osc/common/osc_sp.dart';
import 'package:osc/data/repository/repository.dart';
import 'package:osc/models/entity/osc_oauth_token.dart';
import 'package:osc/models/pop_result/pop_result.dart';
import 'package:osc/res/strings.dart';
import 'package:osc/utils/pd_log.dart';
import 'package:osc/utils/string_util.dart';

import 'navigator_util.dart';

class OscOauth{

  static Future<OscOauthToken> jump2OscAuth(BuildContext context) {
    NavigatorUtil.pushWebForResult<PopResult>(context, url: Constant.OSC_OPEN_API_OAUTH2_AUTHORIZE_URL, title: StringIds.oscOauth).then((result){
      if (result != null) {
        switch (result.id) {
          case PopResult.ID_OSC_OAUTH:
            return _getOauth2Token();
        }
      }
      return null;
    });
  }

  static Future<OscOauthToken> _getOauth2Token() async{
    return await HttpRepository().getOauth2Token(code:OscSp.instance.code).then((resp) {
      if(resp != null){
        if(!StringUtil.isEmpty(resp.accessToken)) {
          PdLog.printLog("------token = " + resp.accessToken);
        }
        if(!StringUtil.isEmpty(resp.errorDescription)) {
          PdLog.printLog("------errorDescription = " + resp.errorDescription);
        }
        return resp;
      }
      return null;
    });
  }
}